package de.hauck.rtmidlet.views;

import java.util.Date;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import org.microemu.app.Config;

import de.hauck.rtmidlet.RTMidlet;
import de.hauck.rtmidlet.model.Configuration;
import de.hauck.rtmidlet.model.StockBean;
import de.hauck.rtmidlet.model.StockStore;

public class FormStockMaintain extends Form implements CommandListener {
	private static final int ADD=0;
	private static final int EDIT=1;
	private Command cCancel = new Command("Cancel", Command.BACK, 1);
	private Command cAddStock = new Command("Add", Command.OK, 1);
	private Command cSaveStock = new Command("Save", Command.OK, 1);

	private TextField name, symbol_yahoo, amount, invest;
	private TextField[] chartSymbols;
	private DateField date;
	private ChoiceGroup listInChoicegroup, listInYahooChoiceGroup;
	private boolean[] selected;

	private StockStore _store;
	private RTMidlet _main;
	private int _stockNumber;
	private Displayable _dble;

	public FormStockMaintain(int stockNumber,
			StockStore myStore, RTMidlet midlet,
			Displayable dble, int action) {
		super("");
		_dble = dble;
		_main = midlet;
		_store = myStore;
		_stockNumber=stockNumber;
		listInChoicegroup = new ChoiceGroup("Show in", ChoiceGroup.MULTIPLE);
		listInYahooChoiceGroup = new ChoiceGroup("Show in Yahoo", ChoiceGroup.MULTIPLE);
		listInYahooChoiceGroup.append("Yahoo", null);
		//for (int i=0; i<Configuration.getAvailableChartTypes().length; i++) {
		//	listInChoicegroup.append(Configuration.getAvailableChartTypes()[i], null);
		//}
		//listInChoicegroup.append("Charts FT", null);
		//listInChoicegroup.append("Overview Yahoo", null);
		chartSymbols = new TextField[Configuration.getAvailableChartTypes().length];

		if (action == ADD) {
			setTitle("Add stock");
			name = new TextField("Name", "Nokia", 15, 0);
			append(name);
			int size = Configuration.getAvailableChartTypes().length;
			boolean[] selected = new boolean[size];
			for (int i=0; i<size; i++) {
				chartSymbols[i]= new TextField("Symbol "+Configuration.getAvailableChartTypes()[i],"NOK",20,TextField.ANY);
				append(chartSymbols[i]);
				listInChoicegroup.append(Configuration.getAvailableChartTypes()[i], null);
				selected[i]=true;
			}
			symbol_yahoo = new TextField("Symbol Yahoo", "NOA3.DE", 20, 0);
			amount = new TextField("Amount", "20", 10, TextField.DECIMAL);
			invest = new TextField("Invest", "1000", 10, TextField.DECIMAL);
			date = new DateField("Buy Date", DateField.DATE);
			date.setDate(new Date(System.currentTimeMillis()));
			
			listInChoicegroup.setSelectedFlags(selected);
			
			append(listInChoicegroup);
			append(symbol_yahoo);
			append(listInYahooChoiceGroup);
			append(amount);
			append(invest);
			append(date);
			
			addCommand(cAddStock);
		} else if (action == EDIT) {
			setTitle("Edit stock");
			deleteAll();
			StockBean stock = (StockBean) _store.getStocksVector().elementAt(
					_stockNumber);
			name = new TextField("Name", stock.getName(), 15, 0);
			append(name);
			for (int i=0; i<Configuration.getAvailableChartTypes().length; i++) {
				String symbol=stock.getChartSymbol(i);
				if (null==symbol) symbol="";
				chartSymbols[i]= new TextField("Symbol "+Configuration.getAvailableChartTypes()[i],symbol,20,TextField.ANY);
				append(chartSymbols[i]);
				listInChoicegroup.append(Configuration.getAvailableChartTypes()[i], null);
			}
			symbol_yahoo = new TextField("Symbol Yahoo",
					stock.getSymbolYahoo(), 20, 0);
			amount = new TextField("Amount", stock.getAmount(), 10, TextField.DECIMAL);
			invest = new TextField("Invest", stock.getInvest(), 10, TextField.DECIMAL);
			date = new DateField("Buy Date", DateField.DATE);
			date.setDate(stock.getBuyDate());
			selected = stock.getDisplayFlags();
			for (int i=0; i<selected.length; i++) {
				if (selected[i]) listInChoicegroup.setSelectedIndex(i, true);
				else listInChoicegroup.setSelectedIndex(i, false);
			}
			//listInChoicegroup.setSelectedFlags(selected);
			listInYahooChoiceGroup.setSelectedIndex(0, stock.getShowInYahoo());
			append(listInChoicegroup);
			append(symbol_yahoo);
			append(listInYahooChoiceGroup);
			append(amount);
			append(invest);
			append(date);
			
			
			addCommand(cSaveStock);
		}
		addCommand(cCancel);
		setCommandListener(this);
		_main.show(this);
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == cCancel) {
			if (_dble instanceof FormStockMaintainMenu) {
			_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
			} else if (_dble instanceof CanvasSearch) {
				_main.show(_dble);
			}
			//_display.setCurrent(_formMaintainMenu.getEditStocksMenu());
		} else if (command == cSaveStock) {
			StockBean editStock = new StockBean(name.getString(), getSymbols(), symbol_yahoo
					.getString(), amount.getString(), invest.getString(), date
					.getDate(), listInYahooChoiceGroup.isSelected(0), getSelectedDisplayFlags());
			
			_store.editStock(editStock, _stockNumber);
			if (_dble instanceof FormStockMaintainMenu) {
				_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
				} else if (_dble instanceof CanvasSearch) {
					_main.show(_dble);
				}
			
			//_display.setCurrent(_formMaintainMenu.getEditStocksMenu());
		} else if (command == cAddStock) {
			StockBean newStock = new StockBean(name.getString(), getSymbols(), symbol_yahoo
					.getString(), amount.getString(), invest.getString(), date
					.getDate(), listInYahooChoiceGroup.isSelected(0) , getSelectedDisplayFlags());
			
			_store.addStock(newStock);
			if (_dble instanceof FormStockMaintainMenu) {
				_main.show(((FormStockMaintainMenu)_dble).getEditStocksMenu());
				} else if (_dble instanceof CanvasSearch) {
					_main.show(_dble);
				}
		}  
	}

	private boolean[] getSelectedDisplayFlags() {
		boolean[] selected = new boolean[listInChoicegroup.size()];
		for (int i = 0; i < listInChoicegroup.size(); i++) {
			if (listInChoicegroup.isSelected(i))
				selected[i] = true;
			else
				selected[i] = false;
		}
		return selected;
	}
	
	
	private String[] getSymbols() {
		String[] names = new String[chartSymbols.length];
		for (int i=0; i<chartSymbols.length; i++) {
			names[i]=chartSymbols[i].getString();
		}
		return names;
	}
}
